package com.michael.self.webcatch

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private lateinit var mWebview: WebView
    private lateinit var etInput: EditText
    private var contentResult = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        etInput= findViewById(R.id.etInput)
        findViewById<TextView>(R.id.tvOpen).setOnClickListener {
            var url=etInput.text.toString()
            if (url.isNotEmpty()){
                if (!url.contains("https")){
                    url="https://"+url
                }
                mWebview.loadUrl(url)
            }
        }
        findViewById<TextView>(R.id.tvDownload).setOnClickListener {

        }
        findViewById<TextView>(R.id.tvCopy).setOnClickListener {
           if (contentResult.isNotEmpty()){
               copyTextToClipboard(this@MainActivity,contentResult)
                Toast.makeText(this@MainActivity,"复制到剪贴板",Toast.LENGTH_SHORT)
           }

        }
        findViewById<TextView>(R.id.tvGet).setOnClickListener {
            mWebview.evaluateJavascript(
                "(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();"
            ) { html -> // 调用之前定义的方法来处理获取的HTML内容
                WebAppInterface().getContent(html!!)
            }

        }
        mWebview = findViewById(R.id.mWebview)
        mWebview.settings.apply {
            javaScriptEnabled = true
            domStorageEnabled = true
        }
        mWebview.webViewClient = object : WebViewClient() {
        }
        mWebview.webChromeClient = object : WebChromeClient() {

        }
        mWebview.addJavascriptInterface(WebAppInterface(), "Android")


        mWebview.loadUrl("https://www.leisu.com/news/catalog-lanqiu");


    }

    fun isUrlValid(url: String?): Boolean {
        val uri = Uri.parse(url)
        return uri.scheme != null
    }


    fun copyTextToClipboard(context: Context, text: CharSequence) {
        val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        clipboard.setPrimaryClip(ClipData.newPlainText("label", text))
    }

    inner class WebAppInterface {
        @JavascriptInterface
        fun getContent(content: String) {
            // 这里处理获取到的内容
            contentResult = extractChineseCharacters(content)
            etInput.setText(contentResult)
        }

        private fun extractChineseCharacters(input: String): String {
            val regex = Regex("[\u4e00-\u9fa5]")
            return regex.findAll(input).joinToString(separator = "") { it.value }
        }


    }


}